#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    Mat src = imread("contour.PNG");
    if (src.empty()) { cout << "read fail\n"; return 0; }


    Mat gray;
    cvtColor(src, gray, COLOR_BGR2GRAY);


    Mat bw;
    threshold(gray, bw, 240, 255, THRESH_BINARY);


    Mat kernel = getStructuringElement(MORPH_RECT, Size(3,3));
    morphologyEx(bw, bw, MORPH_OPEN, kernel);


    vector<vector<Point>> contours;
    findContours(bw, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);


    Mat result = src.clone();
    for (size_t i = 0; i < contours.size(); ++i)
    {
        Rect rc = boundingRect(contours[i]);

        if (rc.area() < 100 || rc.area() > 10000) continue;
        rectangle(result, rc, Scalar(0, 0, 255), 2);
    }

    imshow("white_corners", result);
    waitKey(0);
    return 0;
}
